package wxy.studentmanage.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        System.out.println("123465"+session.getAttribute("user"));
        System.out.println("拦截User");

        if (request.getRequestURI().contains("index")){
            System.out.println("放行index");
            return true;
        }
        if (request.getRequestURI().contains("toLogin")){
            System.out.println("放行toLogin");
            return true;
        }
        if (request.getRequestURI().contains("login")){
            System.out.println("放行login");
            return true;
        }
        if (session.getAttribute("user")!=null){
            System.out.println(session.getAttribute("user"));
            return true;
        }
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
//        if (request.getRequestURI().contains("allUser  toLogin")){
//            System.out.println("放行allUser");
//            return true;
//        }
//        if (request.getRequestURI().contains("del")){
//            System.out.println("放行del");
//            return true;
//        }
//
//        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
